package com.ee.bruscar.generator;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.jar.JarFile;
import java.util.logging.ErrorManager;
import java.util.logging.Logger;

import org.antlr.stringtemplate.CommonGroupLoader;
import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateErrorListener;
import org.antlr.stringtemplate.StringTemplateGroup;
import org.antlr.stringtemplate.StringTemplateGroupLoader;
import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
import org.antlr.stringtemplate.language.DefaultTemplateLexer;
import org.antlr.stringtemplate.test.TestStringTemplate;

import com.ee.bruscar.TemplateRunner;
import com.ee.bruscar.configuration.InterfaceConfiguration;
import com.ee.bruscar.configuration.parser.InterfaceConfigXmlParser;
import com.ee.bruscar.generator.model.TemplateInterface;

public class StubInterfaceGenerator
{
	private Logger log = Logger.getLogger(this.getClass().getName());
	private final String GROUPS_FILE = "classgroup.stg";
	private final String FOLDER = "com/ee/bruscar/templates";
	
	private InterfaceConfiguration config;
	@SuppressWarnings("unused")
	private StringTemplateGroup templates;
	
	
	public StubInterfaceGenerator( File configFile )
	{
		InterfaceConfigXmlParser parser = new InterfaceConfigXmlParser();
		this.config = parser.parse( configFile );
		
	}
	
	public void generate()
	{
		log.info("generate:");
		
    	File templateFileName = new File( getTemplatesPath() );
    	
    	log.info( templateFileName.getPath() );
    	
		StringTemplateGroupLoader loader = new CommonGroupLoader(FOLDER, new ErrorListener() );
		StringTemplateGroup.registerGroupLoader(loader);
		StringTemplateGroup group = StringTemplateGroup.loadGroup("classgroup", DefaultTemplateLexer.class, new StringTemplateGroup("super") );
		StringTemplate template = group.getInstanceOf("classGenerator");
		//template.setAttribute("config", "config" );
		log.info(template.toString() );
		template.setAttribute("config", config );
		
		TemplateInterface interfaceSetup = new TemplateInterface( config.getInterfaceToHonourClass() );
		template.setAttribute("setup", interfaceSetup );
		
		System.out.println(template.toDebugString());
		writeToFile(template);
		
	}
	
	class ErrorListener implements StringTemplateErrorListener
	{

		public void error(String arg0, Throwable arg1)
		{
			log.info(">>error: " + arg0 + " "  );
		}

		public void warning(String arg0)
		{
			log.info(">>warning loading: " + arg0 );
			
		}
		
	}
	
	private String getTemplatesPath()
	{
		return FOLDER + "/" + GROUPS_FILE;
	}

	private String tidyPath( String path )
	{
		return path.replace("test-classes", "classes");
	}
	
	private void writeToFile( StringTemplate template )
	{
		String generatedFileName = StringUtils.convertClassToPath( config.getStubClass() );
		String completeFile = config.getSrcPath() + "/" + generatedFileName;
		
		try 
		{
			BufferedWriter out = new BufferedWriter(new FileWriter(completeFile));
		    out.write( template.toString() );
		    out.close();
		}
		catch (IOException e)
		{
			throw new RuntimeException("Error writing to file: " + e.getMessage() );
		}
	}

}
